import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;

public class InPutModular extends URLClassLoader {


    public InPutModular(URL url) {
        super(new URL[] { url });
    }

    public static Object getModularObject(String str , String cls) {
        try {
            URL url = new URL(str);
            InPutModular t = new InPutModular(url);

            Class c = t.findClass(cls);
            Imodular modular = (Imodular)c.newInstance();

            return modular;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static List<String> getFiles() {
        List<String> files = new ArrayList<String>();
        File file = new File("./modular");
        File[] tempList = file.listFiles();

        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
                if (tempList[i].getName().toLowerCase().endsWith(".jar")){
                    files.add("file:"+tempList[i].toString());
                }
            }
        }
        return files;
    }

    public static List<Imodular> getModularObjects(DmjMain dmjMain){
        List<String> files = getFiles();
        List<Imodular> imodulars = new ArrayList<Imodular>();
        for (String url : files){
            Imodular imodular =(Imodular) getModularObject(url,"ModularMain");
            if (imodular != null){
                imodular.modularInit(dmjMain);
                imodulars.add(imodular);
            }
        }
        return imodulars;
    }
}
